﻿using System.Data;
using System.Linq;
using System.Web.Mvc;
using Pyramid.Models;

namespace Pyramid.Controllers
{
	public class DepartmentController : BaseController
	{
		public ActionResult List()
		{
			var model = Db.Departments.ToList();
			return View(model);
		}

		public ActionResult Create()
		{
			var model = new Department();
			return View("Edit", model);
		}

		public ActionResult Edit(int id)
		{
			var model = Db.Departments.First(x => x.Id == id);
			return View(model);
		}

		[HttpPost]
		public ActionResult Edit(Department model)
		{
			if (model.Id == 0)
			{
				//添加
				Db.Departments.Add(model);
			}
			else
			{
				//修改
				Db.Entry(model).State = EntityState.Modified;
			}
			Db.SaveChanges();
			return JSuccess("操作成功！", "tab_bsi_department");
		}

		public ActionResult Delete(int id)
		{
			var model = new Department {Id = id};
			Db.Entry(model).State = EntityState.Deleted;
			Db.SaveChanges();
			return JSuccess("删除成功！", "tab_bsi_department");
		}
	}
}